package com.ayf.payment.game.api.controller;

import cn.hutool.core.collection.CollUtil;
import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.PageDTO;
import com.ayf.payment.game.api.dto.criteria.MessageBoxCriteria;
import com.ayf.payment.game.api.entity.MessageBox;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.MessageBoxService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 留言箱控制层
 * @Author: zh-liang
 * @Date : 2020-03-06 15:17
 */
@RestController
@RequestMapping("/v/box")
public class MessageBoxController {
    @Autowired
    private MessageBoxService messageBoxService;

    /**
     * 增加留言
     * @param messageBoxCriteria
     * @return
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody MessageBoxCriteria messageBoxCriteria){
        MessageBox messageBox = BeanConvertUtils.convert(messageBoxCriteria, MessageBox.class);
        if (messageBoxService.add(messageBox)) {
            return new CommonResult(CommonConstants.RETURN_SUCCESS,"留言成功");
        }
        return new CommonResult(CommonConstants.RETURN_ERROR,"留言失败");
    }

    /**
     * 增加公众意留言
     * @param messageBoxCriteria
     * @return
     */
    @PostMapping("/addWx")
    public CommonResult addWx(@RequestBody MessageBoxCriteria messageBoxCriteria,HttpServletRequest request){
        messageBoxCriteria.setPartitionName("公众号反馈");
        MessageBox messageBox = BeanConvertUtils.convert(messageBoxCriteria, MessageBox.class);
        if (messageBoxService.addWx(messageBox)) {
            return new CommonResult(CommonConstants.RETURN_SUCCESS,"反馈成功");
        }
        return new CommonResult(CommonConstants.RETURN_ERROR,"反馈失败");
    }

    /**
     * 留言箱查看
     *
     * @param messageBoxCriteria
     * @param merchantDTO
     * @return
     */
    @PostMapping("/list")
    public CommonResult<PageDTO> list(@RequestBody MessageBoxCriteria messageBoxCriteria, MerchantDTO merchantDTO) {
        messageBoxCriteria.setMerchantId(merchantDTO.getId());
        PageHelper.startPage(messageBoxCriteria.getPageNum(), messageBoxCriteria.getPageSize());
        List<MessageBox> messageBoxes = messageBoxService.selectAllMsg(messageBoxCriteria);
        PageInfo<MessageBox> pageInfo = new PageInfo<>(messageBoxes);
        PageDTO boxDTO = BeanConvertUtils.convert(pageInfo, PageDTO.class);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询留言信息成功", boxDTO);
    }

    /**
     * 更改留言状态为已读
     *
     * @param messageBoxCriteria
     * @param merchantDTO
     * @return
     */
    @PostMapping("/updateShow")
    public CommonResult updateShow(@RequestBody MessageBoxCriteria messageBoxCriteria, MerchantDTO merchantDTO) {
        messageBoxCriteria.setMerchantId(merchantDTO.getId());
        if (messageBoxService.updateShow(messageBoxCriteria)) {
            return new CommonResult(CommonConstants.RETURN_SUCCESS, "更改留言状态为已读成功");
        }
        return new CommonResult(CommonConstants.RETURN_ERROR, "更改留言状态为已读失败");
    }

    /**
     * 批量删除留言信息
     */
    @PostMapping("/delMeg")
    public CommonResult<?> delMeg(@RequestBody MessageBoxCriteria messageBoxCriteria, MerchantDTO merchantDTO) {
        if (CollUtil.isEmpty(messageBoxCriteria.getIds())) {
            throw new ParameterException("至少选择一条留言");
        }
        messageBoxCriteria.setMerchantId(merchantDTO.getId());
        if (messageBoxService.delMeg(messageBoxCriteria)) {
            return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "删除成功");
        }
        return new CommonResult<>(CommonConstants.RETURN_ERROR, "删除失败");
    }

    /**
     * 查询留言箱未读信息数量成功
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/selectMsgNum")
    public CommonResult<Integer> selectMsgNum(MerchantDTO merchantDTO) {
        MessageBoxCriteria messageBoxCriteria = new MessageBoxCriteria();
        messageBoxCriteria.setMerchantId(merchantDTO.getId());
        int num = messageBoxService.selectMessageNum(messageBoxCriteria);
        return new CommonResult<Integer>(CommonConstants.RETURN_SUCCESS, "查询留言箱未读信息成功", num);
    }
}
